package com.example.myapplication.view.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.bean.CartBean;
import com.example.myapplication.view.RefactorView.RefreshListView;
import com.example.myapplication.presenter.adapter.CartListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车
 */
public class CartFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private static CartFragment fragment = null;
    private boolean selectAll = false;
    private List<CartBean> list;
    RefreshListView lv;
    CartListAdapter adapter;

    public CartFragment() {
    }

    public static CartFragment newInstance() {
        if (fragment == null) {
            fragment = new CartFragment();
        }
//        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
//        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_cart, container, false);
        intView(v);
        return v;
    }

    private void intView(View v) {
        lv = v.findViewById(R.id.listview);
        ImageView selectAllBtn = v.findViewById(R.id.select_point);
        selectAllBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectAll = !selectAll;
                if (selectAll) {
                    ((ImageView) v).setImageResource(R.drawable.select_red_point);
                } else {
                    ((ImageView) v).setImageResource(R.drawable.select_white_point);
                }
                Log.i("TAG", "size:" + list.size());
                for (int i = 0; i < list.size(); i++) {
                    if (selectAll) {
                        list.get(i).setSelect(true);
                    } else {
                        list.get(i).setSelect(false);
                    }
                    Log.i("TAG", "onClick: " + list.get(i).isSelect());
                }
                adapter.notifyDataSetChanged();
            }
        });
        list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            CartBean cb = new CartBean();
            cb.setId((i + 1) + "");
            cb.setNumber(10 * (i + 1));
            cb.setSelect(false);
            list.add(cb);
        }
        adapter = new CartListAdapter(list);
        lv.setAdapter(adapter);
    }
}